package com.gk.panda.apis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gk.panda.apis.Vo.RoleVo;
import com.gk.panda.apis.dto.RoleDto;
import com.gk.panda.apis.dto.RoleSearchDto;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.RoleService;
import com.gk.panda.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "搜索角色")
    @GetMapping
    public Result<IPage<RoleVo>> searchRoles(RoleSearchDto searchDto){
        IPage<RoleVo> page = roleService.searchRoles(searchDto);
        return Result.ok(page);
    }

    @ApiOperation(value = "增加角色")
    @Log
    @PostMapping
    public Result<String> saveRole(@RequestBody RoleDto roleDto){
        if (roleService.saveRole(roleDto)) {
            return Result.write(200,"添加成功");
        }
        return Result.write(201,"添加失败");
    }

    @ApiOperation(value = "修改角色")
    @Log
    @PutMapping
    public Result<String> updateRole(@RequestBody RoleDto roleDto){
        if (roleService.updateRole(roleDto)) {
            return Result.write(200,"修改成功");
        }
        return Result.write(201,"修改失败");
    }

    @ApiOperation(value = "删除角色")
    @Log
    @DeleteMapping("/{id}")
    public Result<String> deleteRoles(@PathVariable int id){
        int i = roleService.deleteRole(id);
        if (i == -1) return Result.write(201,"有账户使用此角色，无法删除");
        if (i == 1) return Result.write(200,"删除成功");
        return Result.write(201,"删除失败");
    }

}
